package com.ebay.kr.common
{
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.messaging.channels.SecureAMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class RemoteBase
	{
		include "./common.info";
		
		private var _remote:RemoteObject;
		private var _source:String;
		private var _channel:String;
		private var _isSecure:Boolean;
		
		public static var EndPointUri:String = FX_API_PROD_ENDPOINT_URI;
		public static var SecureEndPointUri:String = FX_API_PROD_SECURE_ENDPOINT_URI;
		
		public function RemoteBase(source:String = null)
		{
			_source = source;
			_isSecure = false;
		}
		
		private function init(result:Function, fault:Function):void
		{
			_remote = new RemoteObject(DESTINATION);
			_remote.source = _source;
			_remote.addEventListener(ResultEvent.RESULT, result);
			_remote.addEventListener(FaultEvent.FAULT, fault);
		}
		
		public function getRemoteObject(result:Function, fault:Function):RemoteObject
		{
			var c:AMFChannel = new AMFChannel(CHANNEL_DEFAULT, EndPointUri);
			var cs:ChannelSet = new ChannelSet();
			cs.addChannel(c);
			
			init(result, fault);
			_remote.channelSet = cs;
			
			_isSecure = false;
			
			return _remote;			
		}
		
		public function getRemoteObjectSecure(result:Function, fault:Function):RemoteObject
		{
			var c:SecureAMFChannel = new SecureAMFChannel(CHANNEL_SECURE, SecureEndPointUri);
			var cs:ChannelSet = new ChannelSet();
			cs.addChannel(c);
			
			init(result, fault);
			_remote.channelSet = cs;
			
			_isSecure = true;
			
			return _remote;			
		}
		
		public static function SetEndPoint(isProdEnv:Boolean) : void {
			if (true == isProdEnv) {
				EndPointUri = FX_API_PROD_ENDPOINT_URI;
				SecureEndPointUri = FX_API_PROD_SECURE_ENDPOINT_URI;
			} else {
				EndPointUri = FX_API_SANDBOX_ENDPOINT_URI;
				SecureEndPointUri = FX_API_SANDBOX_SECURE_ENDPOINT_URI;
			}
		}
		
		public static function SetCustomEndPoint(defaultUri:String, secureUri:String) : void {
			EndPointUri = defaultUri;
			SecureEndPointUri = secureUri;
		}
		
		public function set Source(value:String):void
		{
			this._source = value;
		}
		public function get Source():String
		{
			return this._source;
		}
		public function get Remote():RemoteObject
		{
			return this._remote;
		}
		public function get IsSecure():Boolean
		{
			return this._isSecure;
		}
	}
}